package com.niodata.dt.fs;

import java.io.IOException;
import java.net.URI;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.aliyun.oss.AliyunOSSFileSystem;
import org.apache.logging.log4j.util.Strings;

public class OssFileSystemBuilder implements FileSystemBuilder {

  @Override
  public FileSystem createFileSystem(String user, Map<String, String> config)
        throws IOException {
    Configuration conf = new Configuration();
    for (String key : config.keySet()) {
      String value = config.get(key);
      if (Strings.isBlank(value)) {
        continue;
      }
      conf.set(key, value);
    }
    String bucket = config.get("fs.oss.bucket");
    //config.set
    conf.set("fs.defaultFS", "oss://" + bucket);
    AliyunOSSFileSystem fs = new AliyunOSSFileSystem();
    URI uri = URI.create("oss://" + bucket);
    fs.initialize(uri, conf);
    return fs;
  }
}
